package com.example.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.utils.Query;
import com.example.ware.VO.MergeVo;
import com.example.ware.VO.PurchaseDoneVo;
import com.example.ware.dao.WmsWareOrderTaskDao;
import com.example.ware.entity.WmsWareOrderTaskEntity;
import com.example.ware.service.WmsWareOrderTaskDetailService;
import com.example.ware.service.WmsWareOrderTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.ware.entity.WmsPurchaseEntity;
import com.example.ware.service.WmsPurchaseService;
import com.common.utils.PageUtils;
import com.common.utils.R;



/**
 * 采购信息
 *
 * @author WDL
 * @email 2329156214@QQ.com
 * @date 2022-05-01 16:31:00
 */
@RestController
@RequestMapping("gmallware/purchase")
public class WmsPurchaseController {
    @Autowired
    private WmsPurchaseService wmsPurchaseService;
    @Autowired
    private WmsWareOrderTaskService orderTaskService;
    @Autowired
    private WmsWareOrderTaskDao orderTaskDao;


    ///ware/purchase/done
    @PostMapping("/done")
    public R finish(@RequestBody PurchaseDoneVo doneVo) {

        wmsPurchaseService.done(doneVo);

        return R.ok();
    }

    /**
     * 领取采购单
     *
     * @return
     */
    @PostMapping("/received")
    public R received(@RequestBody List<Long> ids) {

        wmsPurchaseService.received(ids);

        return R.ok();
    }


    @PostMapping("/merge")
    public R merge(@RequestBody MergeVo mergeVo) {

        wmsPurchaseService.mergePurchase(mergeVo);
        return R.ok();
    }


    @RequestMapping("/unreceive/list")
    public R unReceiveList(@RequestParam Map<String, Object> params) {
        QueryWrapper<WmsPurchaseEntity> wrapper = new QueryWrapper<>();

        wrapper.in("status", 1, 0);
        List<WmsPurchaseEntity> list = wmsPurchaseService.list(wrapper);
        IPage<WmsPurchaseEntity> page = wmsPurchaseService.page(
                new Query<WmsPurchaseEntity>().getPage(params),
                wrapper
        );

        PageUtils pageUtils = new PageUtils(page);
        return R.ok().put("page", pageUtils);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wmsPurchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WmsPurchaseEntity wmsPurchase = wmsPurchaseService.getById(id);

        return R.ok().put("wmsPurchase", wmsPurchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WmsPurchaseEntity purchase) {
        purchase.setUpdateTime(new Date());
        purchase.setCreateTime(new Date());
        wmsPurchaseService.save(purchase);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WmsPurchaseEntity wmsPurchase){
		wmsPurchaseService.updateById(wmsPurchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wmsPurchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
